package com.otoe.dilidilivid.view.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.otoe.dilidilivid.data.entity.CommentInfo;
import com.otoe.dilidilivid.adapter.CommentListAdapter;
import com.otoe.dilidilivid.R;
import com.otoe.dilidilivid.data.dao.VideoDao;
import com.otoe.dilidilivid.data.entity.VideoInfo;
import com.otoe.dilidilivid.adapter.VideoListAdapter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class VideoPlayActivity extends AppCompatActivity {

    private SurfaceView mSurfaceView;
    private MediaPlayer mMediaPlayer;
    private RecyclerView recyclerView1;
    private RecyclerView recyclerView2;
    private String videoUrl;
    private TextView switch_cr;
    private boolean isComment;
    List<VideoInfo> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_play);
        mSurfaceView = (SurfaceView) findViewById(R.id.surface_view);
        //获取数据
        Intent intent=getIntent();
        videoUrl=intent.getStringExtra("videoUrl");
        init();
        findViewById(R.id.surface_view).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                System.out.println("点击视频");
                if(!mMediaPlayer.isPlaying()){
                    play(videoUrl);
                }else{
                    stop();
                }
            }
        });
        findViewById(R.id.tv_collect).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initDialog();
            }
        });

        @SuppressLint("HandlerLeak")
        final Handler handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                list=(ArrayList<VideoInfo>)msg.obj;
                List<VideoInfo> datas =new ArrayList<>();
                //int videoCover[]={R.drawable.video1,R.drawable.video2,R.drawable.video3,R.drawable.video4,R.drawable.video5, R.drawable.video6,R.drawable.video7,R.drawable.video8,R.drawable.video9,R.drawable.video10,R.drawable.video11, R.drawable.video12,R.drawable.video13,R.drawable.video14,R.drawable.video15,R.drawable.video16,R.drawable.video17, R.drawable.video18,R.drawable.video19,R.drawable.video20};
                int i=0;
      /*          for(VideoInfo videoInfo:list){
                    videoInfo.setImgUrl(videoCover[i]);
                    i++;
                }*/
                Random random=new Random();
                for(int j=0;j<5;j++){
                    datas.add(list.get(random.nextInt(20)));
                }
                recyclerView1 = (RecyclerView) findViewById(R.id.video_list);
                recyclerView1.setLayoutManager(new LinearLayoutManager(VideoPlayActivity.this,LinearLayoutManager.VERTICAL,false));
                recyclerView1.setAdapter(new VideoListAdapter(VideoPlayActivity.this,datas));

            }

        };
        //获取视频
        new Thread(){
            @Override
            public void run(){
                VideoDao videoDao=new VideoDao();
                //videoDao.changeDuration();
                Message msg=Message.obtain();
                msg.obj=videoDao.selectAll();
                handler.sendMessage(msg);
            }
        }.start();

        // Init Comment List
        isComment = false;
        //刚开始评论列表为空
        List<CommentInfo> commentInfos = new ArrayList<CommentInfo>();
        CommentListAdapter commentListAdapter=new CommentListAdapter(commentInfos);
        recyclerView2 = (RecyclerView) findViewById(R.id.comment_list);
        recyclerView2.setLayoutManager(new LinearLayoutManager(VideoPlayActivity.this,LinearLayoutManager.VERTICAL,false));
        recyclerView2.setAdapter(commentListAdapter);

        //评论推荐按钮交换
        switch_cr = findViewById(R.id.switch_comment_recommend);
        switch_cr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!isComment) {
                    switch_cr.setCompoundDrawablesWithIntrinsicBounds(null, getDrawable(R.drawable.recommend), null, null);
                    switch_cr.setText("推荐");
                    recyclerView1.setVisibility(View.INVISIBLE);
                    recyclerView2.setVisibility(View.VISIBLE);
                    findViewById(R.id.comment_edit_bar).setVisibility(View.VISIBLE);//发送评论条
                    isComment=true;
                }else{
                    switch_cr.setCompoundDrawablesWithIntrinsicBounds(null, getDrawable(R.drawable.comment), null, null);
                    switch_cr.setText("评论");
                    recyclerView1.setVisibility(View.VISIBLE);
                    recyclerView2.setVisibility(View.INVISIBLE);
                    findViewById(R.id.comment_edit_bar).setVisibility(View.GONE);
                    isComment=false;
                }
            }
        });

        EditText txt=(EditText) findViewById(R.id.ReviewTextView2);

        //添加评论
        findViewById(R.id.btnReview).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String text=txt.getText().toString();
                Log.e("文本",text);
                commentListAdapter.addItem(text);
                //清空输入框
                txt.setText("");
            }
        });
    }

    private void init() {
        mMediaPlayer = new MediaPlayer();

        mSurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                play(videoUrl);
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }
            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {

            }
        });
    }

    List<VideoInfo> initVideoData(){//模拟推荐视频列表数据
        List<VideoInfo> mdata = new ArrayList<VideoInfo>();
        int i,videoCover[]={R.drawable.video1,R.drawable.video2,R.drawable.video3,R.drawable.video4,R.drawable.video5, R.drawable.video6,R.drawable.video7,R.drawable.video8,R.drawable.video9,R.drawable.video10,R.drawable.video11, R.drawable.video12,R.drawable.video13,R.drawable.video14,R.drawable.video15,R.drawable.video16,R.drawable.video17, R.drawable.video18,R.drawable.video19,R.drawable.video20};
        for(int n=0;n<10;n++){
            i=(int)(Math.random()*20);
            VideoInfo videoInfo = new VideoInfo();
            videoInfo.title="Video"+i;
            videoInfo.uname ="UP"+i;
            videoInfo.playNums =(int)(Math.random()*1000);
            videoInfo.duration=(int)(Math.random()*10)+":"+(int)(Math.random()*60);
            videoInfo.imgUrl =videoCover[i];
            mdata.add(videoInfo);
        }
        return mdata;
    }

    List<CommentInfo> initCommentData(){//模拟评论列表数据
        List<CommentInfo> mdata = new ArrayList<>();
        for(int n=0;n<10;n++){
            int i=(int)(Math.random()*20);
            CommentInfo info = new CommentInfo();
            info.setUserName("User "+ i);
            info.setTime((int)(Math.random()*10)+":"+(int)(Math.random()*60));
            info.setContent("This is a kind comment!");
            mdata.add(info);
        }
        return mdata;
    }

    private void initDialog() {
//        final CommonDialog dialog = new CommonDialog(VideoPlayActivity.this);
//        dialog.setMessage("请选择")
//                .setImageResId(R.mipmap.ic_launcher)
////                .setTitle("系统提示")
//                .setSingle(true).setOnClickBottomListener(new CommonDialog.OnClickBottomListener() {
//                    @Override
//                    public void onPositiveClick() {
//                        dialog.dismiss();
//                        Toast.makeText(VideoPlayActivity.this,"xxxx",Toast.LENGTH_SHORT).show();
//                    }
//
//                    @Override
//                    public void onNegtiveClick() {
//                        dialog.dismiss();
//                        Toast.makeText(VideoPlayActivity.this,"ssss",Toast.LENGTH_SHORT).show();
//                    }
//                }).show();
        startActivity(new Intent(this, CollectionSelectActivity.class));
    }
    //视频停止
    public void stop(){
        if(mMediaPlayer.isPlaying()){
            mMediaPlayer.stop();
        }
    }
    //视频暂停
    public void pasue(){
        if(mMediaPlayer.isPlaying()){
            mMediaPlayer.pause();
        }else{
            mMediaPlayer.start();
        }
    }
    //视频开始播放
    public void play(String videoUrl){
//        String path = "/storage/emulated/0/DCIM/Camera/20170521_200117.mp4";
//        String path="android.resource://"+getPackageName()+"/raw/";
//        String url="https://www.junkaih.com/posts/video10.mp4";
        try {
            mMediaPlayer.reset();
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            //设置需要播放的视频
            mMediaPlayer.setDataSource(videoUrl);

            //把视频画面输出到SurfaceView
            mMediaPlayer.setDisplay(mSurfaceView.getHolder());
            mMediaPlayer.prepare();
            mMediaPlayer.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}